import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Exercises {
    private int count=0;
    private int low;
    private int high;
    Random random=new Random();
    Set<Equation>equations=new HashSet<>();
    public Exercises(){}
    public Exercises(int low,int high,int number)
    {
        generativeExercises(low,high,number);
    }
    int random_(int low,int high)
    {
        return low+random.nextInt(high-low+1);
    }
    boolean judge(int number,int low,int high)
    {
        return number>=low&&number<=high;
    }
    public void clear()
    {
        equations.clear();
    }
    public Equation generateAdditionEquation(int low,int high)
    {
        Equation eq=new Equation();
        eq.operate='+';
        do{
            eq.left=random_(low,high);
            eq.right=random_(low,high);
        }while(!judge(eq.left+eq.right,low,high));
        return eq;
    }
    public Equation generateSubtractionEquation(int low,int high)
    {
        Equation eq=new Equation();
        eq.operate='-';
        do{
            eq.left=random_(low,high);
            eq.right=random_(low,high);
        }while(!judge(eq.left-eq.right,low,high));
        return eq;
    }
    public Equation generateAdditionEquation(int left,int right,int low,int high)
    {
        Equation eq=new Equation();
        eq.left=left;
        eq.right=right;
        eq.operate='+';
        if(!judge(left,low,high)){
            System.out.println("left异常");
            return null;
        }
        if(!judge(right,low,high)){
            System.out.println("right异常");
            return null;
        }
        if(!judge(left+right,low,high)){
            System.out.println("加法结果异常");
            return null;
        }
        return eq;
    }
    public Equation generateSubtractionEquation(int left,int right,int low,int high)
    {
        Equation eq=new Equation();
        eq.left=left;
        eq.right=right;
        eq.operate='-';
        if(!judge(left,low,high)){
            System.out.println("left异常");
            return null;
        }
        if(!judge(right,low,high)){
            System.out.println("right异常");
            return null;
        }
        if(!judge(left-right,low,high)){
            System.out.println("减法结果异常");
            return null;
        }
        return eq;
    }
    public void generativeExercises(int low,int high,int number)
    {

        this.low=low;
        this.high=high;
        while(equations.size()<number) {
            boolean flag = random.nextBoolean();
            if (flag) {
                equations.add(generateAdditionEquation(low, high));
            } else equations.add(generateSubtractionEquation(low, high));
        }
    }
    public void testEquationNumberForPrint(int low,int high,int number)
    {
        this.clear();
        System.out.println("Test:"+(++count));
        this.generativeExercises(low,high,number);
        this.printEA();
        System.out.println();
        this.clear();
    }
    public void printExercises()
    {
        System.out.println("----------------------------------------------------------------------------------------------------------------------------------------");
        System.out.println("-习题");
        System.out.println("----------------------------------------------------------------------------------------------------------------------------------------");
        int i=0;
        for (Equation eq:equations)
        {
            System.out.printf("%-3d: %s    ",++i,eq.toString_(high));
            if(i%6==0) System.out.println();
        }
        System.out.println();
    }
    public void printAnswer()
    {
        System.out.println("--------------------------------------------------------------------");
        System.out.println("-答案");
        System.out.println("--------------------------------------------------------------------");
        int i=0;
        for (Equation eq:equations)
        {
            System.out.printf("%-3d: %-3d    ",++i,eq.value());
            if(i%6==0) System.out.println();
        }
        System.out.println();
    }
    public void printEA()
    {
        this.printExercises();
        this.printAnswer();
    }
}
